<?
	  layout("/inc/win_layout.html",{title:sysTitle}){
    ?>

<div class="col-lg-4">
	<div class="panel" style="height:100%; width:100%; overflow: auto;">
		<div class="panel-heading">字典项管理</div>
		<div class="panel-heading" >
			<span class="subtitle">
				<button type="button" onclick="addUpdateDictItem('0')" class="btn btn-primary btn-green"  style="width:80px;"
					id="savejiedian">新&nbsp;&nbsp;建</button>&nbsp;&nbsp;
				<button type="button"  class="btn btn-primary btn-green"  style="width:80px;"
					id="editjiedian">编&nbsp;&nbsp;辑</button>&nbsp;&nbsp;
				<button type="button" onclick="" class="btn btn-primary btn-green"  style="width:80px;"
					id="deletejiedian">删&nbsp;&nbsp;除</button>&nbsp;&nbsp;
				<button type="button" onclick="delAll()" class="btn btn-primary btn-green"  style="width:80px;">删除全部</button>
			</span>
		</div>
		<div class="panel-body" style="height: 396px;">
			<ul id="tree" class="ztree" style="width:360px;overflow: auto;">
				
			</ul>
			<div id="dataNull" 
					style="height: 90px; line-height: 50px;" align="center">
			</div>
		</div>
		
	</div>
</div>


<?} ?>

<script>
	var dictId = window.parent.getlay();
	var index = parent.layer.getFrameIndex(window.name); 
	var parentname=1;
	var id;
	function getaddupate() {
		return id;
	}

	function getDictId() {
		return dictId;
	}
	
	//树状结构
	$(function() {
		/* initTree(); */
		loadOneTree();
	});

	var classid;
	var classname;
	var sum;
	var sortsum;
	var treeNodeOne = '';
	var treeId;
	
	function getclassname(){
		return classname;
	}
	function getclassid() {
		return classid;
	}
	function gettreeNode() {
		return treeNodeOne;
	}
	function getsum() {
		return sum+1;
	}
	function getsortsum() {
		return sortsum;
	}
	
	var treeSetting = {
		async: {
			enable: true, url:"./findallDictItemZTreeAsy.do",
			dataType:"json",
			autoParam:["id","dictId","value","level"]
		},
		view: {
		dblClickExpand: true,
		selectedMulti: false,
		expandSpeed: ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast"
		},
		/* view : {  
            addHoverDom: addHoverDom,       // 用于当鼠标移动到节点上时，显示用户自定义控件。务必与 setting.view.removeHoverDom 同时使用  
            removeHoverDom: removeHoverDom, // 用于当鼠标移出节点时，隐藏用户自定义控件。务必与 addHoverDom 同时使用  
            dblClickExpand : false,  
            selectedMulti : false  
        }, */
		data: {
		  simpleData: {
		    enable:true
		  }
		},
		callback: {
			onClick : onClick
		 /*  beforeExpand: beforeExpand */
		}
	};
	
	function loadOneTree(){
		$.ajax({
			type: "post",
			url: "loadOneTree.do",
			data: {
				"dictId": dictId
			},
			dataType: "json",
			success: function(data) {
				if(data[0].msg=="OK"){
					/* for(var i=0;i<data[0].data.length;i++){
						rootNode+='{"id":"'+data[0].data[i].id+'", "pid":"1","dictId":"'+data[0].data[i].dictId+'", "level":"'+data[0].data[i].level+'","name":"'+data[0].data[i].name+'","value":"'+data[0].data[i].value+'","open":false, "isParent":'+data[0].data[i].isParent+'},'
					} */
					loadTree(data[0].data);
				}else{
					alert("请求失败");
				}
			},
			error : function(data) {
				// alert("请求失败");
			}
		});
	}
	
	function loadTree(rootNode){
		var zTreeObj = $.fn.zTree.init($("#tree"), treeSetting, rootNode);
		zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.expandNode(zTree.getNodes()[0], false);//指定某节点折叠
		/* var node = zTreeObj.getNodeByParam("id", 1, null);
		zTreeObj.reAsyncChildNodes(node, "refresh"); */
	}
	
	/* function initTree() {
		$.ajax({
			type : "post",
			data : {
				"dictId" : dictId
			},
			url : "./findallDictItemZTree.do",
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : drawTree,
		});
	}

	function drawTree(data) {
		if(data!="" && data!=null){
			$("#dataNull").html("");
		}else{
			$("#dataNull").html("<span class='subtitle' style='font-size: 15px;'>数据为空!</span>");
		}
	
		var setting = {
			view : {
				dblClickExpand : false,
			},
			data : {
				simpleData : {
					enable : true,
				}
			},
			check : {
				enable : false
			},
			callback : {
				onClick : onClick
			},
		}; 
		
		var zNodes = eval(data);
		
		$.fn.zTree.init($("#tree"), setting, zNodes).expandAll(true);
		zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.expandNode(zTree.getNodes()[0], true);//指定某节点折叠
		var zTreeResult = zTree.getNodes();
		sum=zTreeResult.length;
		sortsum=-1;
		classid = "";
		classname="";
	}*/
	
	/* // 用于当鼠标移出节点时，隐藏用户自定义控件  
	function removeHoverDom(treeId, treeNode) {  
	    $("#addBtn_"+treeNode.tId).unbind().remove();  
	};
	var newCount =1;
	function addHoverDom(treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
		var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
			+ "' title='add node' onfocus='this.blur();'></span>";
		sObj.after(addStr);
		var btn = $("#addBtn_"+treeNode.tId);
		if (btn) btn.bind("click", function(){
			alert(treeNode.id)
			addUpdateDictItem(treeNode.id)
			var zTree = $.fn.zTree.getZTreeObj("tree");
			zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
			return false;
		});
	}; */
	
	function onClick(e, treeId, treeNode) {
	
		sortsum=treeNode.value;
		//alert(sortsum);
		if(typeof treeNode.children == 'undefined'){
			sum = 0;
		}else{
			sum=treeNode.children.length;
		}
		classid = treeNode.id;
		classname=treeNode.name;
		treeNodeOne = treeNode;
		 if(treeNode.children==null)
        {
       			parentname=0;
        }else{
         		parentname=1;
        }
		console.log(treeNodeOne);
		/* treeNode = JSON.stringify(treeNode); */
	}
	function showTree(data,treeNode){
		var zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.addNodes(treeNode, data);
	}
	
	$("#deletejiedian").click(function () { 
		if(classid!=null){
			del(classid);
		}else{
			alert("请选中节点");
		}
	});
	function del(id) {
		if(parentname==0)
		{
		/* var zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.selectNode(treeNodeOne); */
		var isDel=confirm("是否确认删除！！");  
			if (isDel)
			{
				$.ajax({
					type : "post",
					url : "./deldictiem.do",
					data : {
						"id" : id
					},
					dataType : "json",
					success : function(data) {
						if(data.count==1){
							layer.msg('删除成功！');
							refreshParentNode();
						}else{
							layer.msg('有关联,不可删除！');
						}
						/* loadOneTree(); */
						/* initTree(); */
					},
					error : function(data) {
						alert("请求失败");
					}
				});
			}
		}else{
			 alert("有子节点不允许删除");
		}
	}
	//删除所有
	function delAll() {
			if (confirm("是否确认删除所有！！"))
			{
				$.ajax({
					type : "post",
					url : "./deldictiemAll.do",
					data : {
						"id" : dictId
					},
					dataType : "json",
					success : function(data) {
						if(data.count==1){
							layer.msg('删除成功！');
							
						}
						loadOneTree();
						/* initTree(); */
					},
					error : function(data) {
						alert("请求失败");
					}
				});
			}
	}
	
	$("#editjiedian").click(function () { 
		if(classid!=null){
			addUpdateDictItem(classid);
		}else{
			alert("请选中节点！！");
		}
	});
	
	
	function addUpdateDictItem(lid) {
		id = lid;
		layer.open({
			type : 2,
			title : '字典项管理',
			scrollbar : false,
			shadeClose : true,
			move : false,
			area : [ '550px', '500px' ],
			fix : false, //不固定
			//maxmin : true,
			skin : 'layui-layer-rim', //加上边框
			btn : [ '保存 ', '关闭' ],
			content : ["dictItem-add-updateZTree.html",'no'],
			yes: function(index,layer){
                 //var iframeWin = parent.window[layer.find('iframe')[0]['name']].window[layer.find('iframe')[0]['name']];
                 var iframeWin = window.parent.window[window.name].window['layui-layer-iframe'+index];
                 iframeWin.Page();
  	        },
				cancel : function(index) {
			}
		});
	};
	/** 
	  * 刷新当前节点 
	  */
	 function refreshNode() { 
	  /*根据 treeId 获取 zTree 对象*/
	  var zTree = $.fn.zTree.getZTreeObj("tree"), 
	  type = "refresh", 
	  silent = false, 
	  /*获取 zTree 当前被选中的节点数据集合*/
	  nodes = zTree.getSelectedNodes(); 
	  /*强行异步加载父节点的子节点。[setting.async.enable = true 时有效]*/
	  zTree.reAsyncChildNodes(nodes[0], type, silent); 
	 } 
	  /** 
  * 刷新当前选择节点的父节点 
  */
 function refreshParentNode() { 
	  var zTree = $.fn.zTree.getZTreeObj("tree"), 
	  type = "refresh", 
	  silent = false, 
	  nodes = zTree.getSelectedNodes(); 
	  /*根据 zTree 的唯一标识 tId 快速获取节点 JSON 数据对象*/
	  var parentNode = zTree.getNodeByTId(nodes[0].parentTId); 
	  /*选中指定节点*/
	  zTree.selectNode(parentNode); 
	  zTree.reAsyncChildNodes(parentNode, type, silent); 
	}
	 
</script>